package cn.ht.jywrwater.module.ht.controller.admin.waterleakageanalysis;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDate;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.waterleakageanalysis.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterleakageanalysis.WaterLeakageAnalysisDO;
import cn.ht.jywrwater.module.ht.convert.waterleakageanalysis.WaterLeakageAnalysisConvert;
import cn.ht.jywrwater.module.ht.service.waterleakageanalysis.WaterLeakageAnalysisService;

@Tag(name = "管理后台 - 漏损分析")
@RestController
@RequestMapping("/ht/water-leakage-analysis")
@Validated
public class WaterLeakageAnalysisController {

    @Resource
    private WaterLeakageAnalysisService waterLeakageAnalysisService;

    @PostMapping("/create")
    @Operation(summary = "创建漏损分析")
    @PreAuthorize("@ss.hasPermission('ht:water-leakage-analysis:create')")
    public CommonResult<Long> createWaterLeakageAnalysis(@Valid @RequestBody WaterLeakageAnalysisCreateReqVO createReqVO) {
        return success(waterLeakageAnalysisService.createWaterLeakageAnalysis(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新漏损分析")
    @PreAuthorize("@ss.hasPermission('ht:water-leakage-analysis:update')")
    public CommonResult<Boolean> updateWaterLeakageAnalysis(@Valid @RequestBody WaterLeakageAnalysisUpdateReqVO updateReqVO) {
        waterLeakageAnalysisService.updateWaterLeakageAnalysis(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除漏损分析")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-leakage-analysis:delete')")
    public CommonResult<Boolean> deleteWaterLeakageAnalysis(@RequestParam("id") Long id) {
        waterLeakageAnalysisService.deleteWaterLeakageAnalysis(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得漏损分析")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-leakage-analysis:query')")
    public CommonResult<WaterLeakageAnalysisRespVO> getWaterLeakageAnalysis(@RequestParam("id") Long id) {
        WaterLeakageAnalysisDO waterLeakageAnalysis = waterLeakageAnalysisService.getWaterLeakageAnalysis(id);
        return success(WaterLeakageAnalysisConvert.INSTANCE.convert(waterLeakageAnalysis));
    }

    @GetMapping("/list")
    @Operation(summary = "获得漏损分析列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-leakage-analysis:query')")
    public CommonResult<List<WaterLeakageAnalysisRespVO>> getWaterLeakageAnalysisList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterLeakageAnalysisDO> list = waterLeakageAnalysisService.getWaterLeakageAnalysisList(ids);
        return success(WaterLeakageAnalysisConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得漏损分析分页")
    @PreAuthorize("@ss.hasPermission('ht:water-leakage-analysis:query')")
    public CommonResult<PageResult<WaterLeakageAnalysisRespVO>> getWaterLeakageAnalysisPage(@Valid WaterLeakageAnalysisPageReqVO pageVO) {
        PageResult<WaterLeakageAnalysisDO> pageResult = waterLeakageAnalysisService.getWaterLeakageAnalysisPage(pageVO);
        return success(WaterLeakageAnalysisConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出漏损分析 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-leakage-analysis:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterLeakageAnalysisExcel(@Valid WaterLeakageAnalysisExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterLeakageAnalysisDO> list = waterLeakageAnalysisService.getWaterLeakageAnalysisList(exportReqVO);
        // 导出 Excel
        List<WaterLeakageAnalysisExcelVO> datas = WaterLeakageAnalysisConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "漏损分析.xls", "数据", WaterLeakageAnalysisExcelVO.class, datas);
    }

    @GetMapping("/analysisList")
    @Operation(summary = "获得漏损分析列表")
    @Parameter(name = "region", description = "区域", required = true, example = "1024")
    @Parameter(name = "beginDate", description = "开始时间", required = true, example = "2024-01-25")
    @Parameter(name = "endDate", description = "截止时间", required = true, example = "2024-01-25")
    @PreAuthorize("@ss.hasPermission('ht:water-leakage-analysis:query')")
    public CommonResult<WaterLeakageAnalysisVO> getAnalysisList(@RequestParam("region") String region,
                                                                @RequestParam("beginDate") @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate beginDate,
                                                                @RequestParam("endDate") @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate endDate) {
        WaterLeakageAnalysisVO result = waterLeakageAnalysisService.getAnalysisList(region,beginDate,endDate);
        return success(result);
    }

}
